﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.geom.ColorTransform;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.text.TextField;
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.shadematerials.GouraudMaterial;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;
	import org.papervision3d.events.FileLoadEvent;
	
	/*
	 * coded by moka 2009 / 06/ 20 
	 * name : moka's gwagwaken
	 */
	
	public class Main extends Sprite 
	{		
		// 基本 PV3D 操作請參考 PV3D 基本教學
		private var scene:Scene3D = new Scene3D;
		private	var camera:Camera3D = new Camera3D;
		private	var viewport:Viewport3D = new Viewport3D(640, 480);
		private	var render:BasicRenderEngine = new BasicRenderEngine;
		private var bmp:Bitmap = new Bitmap(new BitmapData(640, 480, true, 0));
		private var bmpBuffer:BitmapData = bmp.bitmapData.clone();
		private var torus:MokaMax3DS;

		public function Main():void 
		{		
			// 設定場景不變形
			stage.scaleMode = "noScale"; 
			addChild(bmp);
	
			// 新增 Autodesk 3DMax 輸出的 3ds 檔案 torus
			torus = new MokaMax3DS("torus");
			torus.load("torus.3ds");
			torus.addEventListener(FileLoadEvent.LOAD_COMPLETE, onload);
			scene.addChild(torus);
			
			// Moka's work 2009
			(addChild(new TextField()) as TextField).htmlText = "<font color='#FFFFFF'>Moka's work 2009</font>"; 
		}		
		
		private function onload(e:FileLoadEvent):void
		{	
			// torus 大小與材質設定
			torus.scale = 8;
			torus.getModel().material = new GouraudMaterial(new PointLight3D);
			//torus.getModel().geometry 亦可修改模型頂點座標等
			
			stage.addEventListener(Event.ENTER_FRAME, onProcess);
		}
		
		private function onProcess(e:Event):void {
			torus.rotationX = mouseX;
			torus.rotationY = mouseY;
			render.renderScene(scene, camera, viewport);
			bmpBuffer.fillRect(bmpBuffer.rect, 0x33000000);
			bmpBuffer.draw(viewport);
			bmp.bitmapData.draw(bmpBuffer);
		}   
	}	
}